package ThreadTest;

public class BankTest1 {
    public static void main(String[] args) {
        Bank bank = new Bank();
//        for (int i = 0; i < 100; i++) {
//            bank.getInstance();
//        }
        new Thread(()->bank.getInstance()).start();
        new Thread(()->bank.getInstance()).start();
        new Thread(()->bank.getInstance()).start();
        new Thread(()->bank.getInstance()).start();
        new Thread(()->bank.getInstance()).start();
        new Thread(()->bank.getInstance()).start();
        new Thread(()->bank.getInstance()).start();
        new Thread(()->bank.getInstance()).start();
        new Thread(()->bank.getInstance()).start();
        new Thread(()->bank.getInstance()).start();
        new Thread(()->bank.getInstance()).start();
        new Thread(()->bank.getInstance()).start();
        new Thread(()->bank.getInstance()).start();
        new Thread(()->bank.getInstance()).start();
        new Thread(()->bank.getInstance()).start();
        new Thread(()->bank.getInstance()).start();
        new Thread(()->bank.getInstance()).start();
        new Thread(()->bank.getInstance()).start();
        new Thread(()->bank.getInstance()).start();
        new Thread(()->bank.getInstance()).start();
        new Thread(()->bank.getInstance()).start();
        new Thread(()->bank.getInstance()).start();
        new Thread(()->bank.getInstance()).start();
        new Thread(()->bank.getInstance()).start();
        new Thread(()->bank.getInstance()).start();
        new Thread(()->bank.getInstance()).start();






    }


}

class Bank {

    Bank(){}
    private static Bank instance = null;

    public static Bank getInstance() {
        synchronized (Bank.class) {
            if (instance == null) {
                System.out.println("6");
                instance = new Bank();
            }
            return instance;
        }
    }
    // 效率较上面的较高（先：两个以上的线程同时判断null，进行判断； 后：其余线程判断null，不会进入到锁机制）
    public static Bank getInstance1() {
        if (instance == null) {
            synchronized (Bank.class) {
                if (instance == null) {
                    System.out.println("6");
                    instance = new Bank();
                }
            }
        }
        return instance;
    }
}
